// NetValueConverter.cs
//
// Copyright (c) 2007 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

using Microsoft.Practices.ServiceLocation;

using Supremacy.Diplomacy;
using Supremacy.Entities;
using Supremacy.Game;
using Supremacy.Types;

namespace Supremacy.Client
{
    [ValueConversion(typeof(int), typeof(Brush))]
    public class NetValueBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((int)value < 0)
                return Brushes.Red;
            return parameter;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    [ValueConversion(typeof(int), typeof(String))]
    public class NetValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((int)value > 0)
                return "+" + value;
            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Number.ParseInt32(value.ToString());
        }
    }

    [ValueConversion(typeof(object), typeof(Brush))]
    public class RelationshipStatusBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var defaultBrush = Brushes.Transparent;

            var civ = value as Civilization;
            if (civ != null)
            {
                var clientContext = ServiceLocator.Current.GetInstance<IClientContext>();
                if (clientContext == null)
                    return defaultBrush;
                
                if (civ.CivID == clientContext.LocalPlayer.EmpireID)
                    return defaultBrush;

                var playerCiv = clientContext.LocalPlayer.Empire;
                if (playerCiv == null)
                    return defaultBrush;

                if (!DiplomacyHelper.IsContactMade(playerCiv, civ))
                    return defaultBrush;

                value = GameContext.Current.DiplomacyStatus[clientContext.LocalPlayer.EmpireID, civ.CivID];
            }

            if (value is RelationshipStatus)
            {
                switch ((RelationshipStatus)value)
                {
                    case RelationshipStatus.Self:
                    case RelationshipStatus.Unknown:
                        return defaultBrush;
                    case RelationshipStatus.Affiliated:
                        return Brushes.MediumTurquoise;
                    case RelationshipStatus.Allied:
                        return Brushes.DodgerBlue;
                    case RelationshipStatus.AtWar:
                        return Brushes.Crimson;
                    case RelationshipStatus.Peace:
                    case RelationshipStatus.Friendly:
                        return Brushes.LimeGreen;
                    case RelationshipStatus.Neutral:
                        return Brushes.Silver;
                    case RelationshipStatus.OtherIsMember:
                    case RelationshipStatus.OwnerIsMember:
                    case RelationshipStatus.OtherIsProtectorate:
                    case RelationshipStatus.OwnerIsProtectorate:
                        return Brushes.Violet;
                    case RelationshipStatus.OtherIsSubjugated:
                    case RelationshipStatus.OwnerIsSubjugated:
                        return Brushes.Orange;
                }
            }
            else if (value is DiplomacyStatus)
            {
                switch ((DiplomacyStatus)value)
                {
                    case DiplomacyStatus.Alliance:
                        return Brushes.DeepSkyBlue;
                    case DiplomacyStatus.War:
                        return Brushes.Crimson;
                    case DiplomacyStatus.Peace:
                        return Brushes.LimeGreen;
                    case DiplomacyStatus.Membership:
                        return Brushes.Violet;
                    case DiplomacyStatus.Subjugation:
                        return Brushes.Orange;
                    case DiplomacyStatus.Neutral:
                        return Brushes.Silver;
                    default:
                    case DiplomacyStatus.NoContact:
                        return defaultBrush;
                }
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}
